# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=libavro
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.11.1
pkgrel=1
pkgdesc="The Apache Avro data serialization system (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://github.com/apache/avro"
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-asciidoc"
             "${MINGW_PACKAGE_PREFIX}-source-highlight")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-jansson"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/apache/avro/archive/release-${pkgver}.tar.gz"
        "001-libavro-1.10.0-fix-mingw-build.patch"
        "002-libavro-1.10.0-add-threadsafe-option.patch")
sha256sums=('599f96bb405f72a35154b2477caa6254d723bb4e3f6a0e54e9ae540664321752'
            'e4c7648ef90268a9ce561e6a4028ff63a206f28e82fe8f4dc01630825b266761'
            '346e37dc9adb52739e672483ee895299da6e00121ae0061beb2df3063234d62b')

prepare() {
  cd "${srcdir}/avro-release-${pkgver}"
  patch -Np1 -i "${srcdir}/001-libavro-1.10.0-fix-mingw-build.patch"
  patch -Np1 -i "${srcdir}/002-libavro-1.10.0-add-threadsafe-option.patch"
}

build() {
  for lang in c c++
  do
    rm -rf "${srcdir}/build-${MSYSTEM}-${lang}"
    mkdir "${srcdir}/build-${MSYSTEM}-${lang}"
    cd "${srcdir}/build-${MSYSTEM}-${lang}"

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE="Release" \
      -DTHREADSAFE=ON \
      "../avro-release-${pkgver}/lang/${lang}"

    ${MINGW_PREFIX}/bin/cmake --build .
  done
}

check() {
  for lang in c c++
  do
    cd "${srcdir}/build-${MSYSTEM}-${lang}"
    ${MINGW_PREFIX}/bin/ctest
  done
}

package() {
  for lang in c c++
  do
    cd "${srcdir}/build-${MSYSTEM}-${lang}"
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  done
  rm "${pkgdir}${MINGW_PREFIX}"/include/avro/{msinttypes,msstdint}.h
}
